package com.wangshuo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshuo.config.Result;
import com.wangshuo.domain.SysRole;
import com.wangshuo.domain.SysUser;

import com.wangshuo.domain.SysUserRole;
import com.wangshuo.model.vo.AssginRoleVo;
import com.wangshuo.model.vo.SysUserQueryVo;
import com.wangshuo.service.SysRoleService;
import com.wangshuo.service.SysUserRoleService;
import com.wangshuo.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Tag(name="用户管理")
@RequestMapping("/user")
public class UserController {
    @Resource
    private SysUserService service;
    @Resource
    private SysRoleService roleService;
    @Resource
    private SysUserRoleService userRoleService;
    @PostMapping("/userlist")
    @Operation(summary = "分页用户")
    public Result getUserList(@RequestParam long pageNum, @RequestParam long pageSize, @RequestBody SysUserQueryVo sysUserQueryVo){

        IPage<SysUser> page=new Page<>(pageNum,pageSize);
        IPage<SysUser> list=service.getlist(page,sysUserQueryVo);
        return Result.ok(list);
    }
    @GetMapping("/getbyId")
    @Operation(summary = "通过ID获取用户")
    public Result getbyid(@RequestParam long id){
        SysUser user=service.getById(id);
        return Result.ok(user);
    }
    @PostMapping("/save")
    @Operation(summary = "保存更新用户")
    public Result save(@RequestBody SysUser sysUser){
        service.saveOrUpdate(sysUser);
        return Result.ok("成功");
    }
    @GetMapping("/remove")
    @Operation(summary = "删除")
    public Result remove(@RequestParam long id){
        service.removeById(id);
        return Result.ok("成功");
    }
    @GetMapping("/toAssign")
    @Operation(summary = "根据用户获取角色数据")
    public Result toAssign(@RequestParam long id){
        HashMap<String,Object> map=new HashMap<>();
        List<SysRole> list=roleService.list();
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRole::getUserId,id);
        List<SysUserRole> userRoles=userRoleService.list(lambdaQueryWrapper);
        List<Long> list1=new ArrayList<>();
        for (SysUserRole sysUserRole:userRoles){
            list1.add(sysUserRole.getRoleId());
        }
        map.put("allroles",list);
        map.put("userroles",list1);
        return Result.ok(map);
    }
    @PostMapping("/addAssign")
    public Result addAssign(@RequestBody AssginRoleVo assginRoleVo){
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRole::getUserId,assginRoleVo.getUserId());
        userRoleService.remove(lambdaQueryWrapper);


        for (String id:assginRoleVo.getRoleIdList()){
       SysUserRole sysUserRole=new SysUserRole();
       sysUserRole.setUserId(Long.valueOf(assginRoleVo.getUserId()));
       sysUserRole.setRoleId(Long.valueOf(id));
       userRoleService.save(sysUserRole);
        }
        return Result.ok("成功");
    }
}
